#include <string>
#include <CSPProcess.h>
#include <csp_debug.h>
#include <csp_misc.h>
#include <ProcessTerminationEvent.h>
#include <ProcessComposition.h>
#include <ProcessStats.h>

using namespace std;

CSPProcess::CSPProcess(string mnemonic_)
{
	mnemonic = mnemonic_;

	p_comp = new ProcessComposition(mnemonic, NULL, NULL);		

	isRun = false;
}

string &
CSPProcess::GetMnemonic(){
	return mnemonic;
}


int 
CSPProcess::Run()
{
	if( isRun ){
		Failure();
	}

	p_event = new ProcessTerminationEvent(1);

	p_comp->SetEvent(p_event, NULL);

	p_comp->Schedule();
	isRun = true;

	return p_event->WaitForProcessTermination();
}

void
CSPProcess::Failure()
{
	printf("Process can only be executed once.\n");
	exit(-1);
}
